package com.yinhe.music.yhmusic.personal;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.UserInfo;
import com.yinhe.music.yhmusic.pay.PayActivity;
import com.yinhe.music.yhmusic.personal.adapter.SnowIconAdapter;
import com.yinhe.music.yhmusic.personal.model.VipListInfo;
import com.yinhe.music.yhmusic.personal.vip.ProductListContract;
import com.yinhe.music.yhmusic.personal.vip.ProductPresenter;
import com.yinhe.music.yhmusic.service.PlayService;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.List;

import butterknife.BindView;

/**
 * Created by 何晓华 on 2017/9/19.
 */

public class MyWalletActivity extends BaseServiceActivity implements BaseQuickAdapter.OnItemChildClickListener, ProductListContract.IProductListView {

    @BindView(R.id.balance)
    TextView balance;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.wallet_layout)
    RelativeLayout walletLayout;

    private SnowIconAdapter mAdapter;

    ProductPresenter mPresenter;

    @Override
    public void initView() {
        RxBus.get().register(this);
        setToolbar("我的雪币");
        setRecyclerView();
        showQuickControl(true);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_my_wallet;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new ProductPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onServiceConnected(PlayService service) {
        super.onServiceConnected(service);
        getIconList();
    }

    private void setRecyclerView() {
        mAdapter = new SnowIconAdapter();
        mAdapter.setOnItemChildClickListener(this);

        WrapContentLinearLayoutManager layoutManager = new WrapContentLinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST, 0, true));
    }

    private void getIconList() {
        mPresenter.getProductList(IConstants.PRODUCT_SNOW_GOLD);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (view.getId() == R.id.open) {
            VipListInfo products = (VipListInfo) adapter.getData().get(position);
            Intent intent = new Intent(this, PayActivity.class);
            intent.putExtra("price", String.valueOf(products.getPrice()));
            intent.putExtra("productstype", IConstants.PRODUCT_SNOW_GOLD);
            intent.putExtra("products", String.valueOf(products.getSnowGold()));
            startActivity(intent);
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Pay.PAY_STATUS)
    )
    public void onRxbusPayStatus(Integer payStatus) {
        LogUtils.d(payStatus);
        getIconList();
    }

    @Override
    public void setProductListUI(VipListInfo list) {
        List<VipListInfo> mList = list.getSnowGoldInfo();
        mAdapter.setNewData(mList);
        balance.setText(String.valueOf(list.getBalance()));
    }

    @Override
    public void setUserUI(UserInfo userInfo) {

    }
}
